<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

Class Category extends Record {
    const TABLE_NAME = 'category';
    public $id;
    public $category_name;
    public $category_description;
    public $tags;
    public $visits;

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->category_name;
    }

    public function getDescription() {
        return $this->category_description;
    }

    public function getTags() {
        // return explode(',', $this->tags);
        return $this->tags;
    }

    public function setId($id) {
        $this->id = $id;
    }
    
    public function setName($name) {
        $this->category_name = $name;
    }

    public function setDescription($description) {
        $this->category_description = $description;
    }

    public function setTags($tags) {
        $this->tags = $tags;
    }

    public function addTag($tag) {
        $this->tags .= ',' . $tag;
    }

    public function isTag($tag) {
        $tags = explode(',', $this->tags);
        return is_array($tag, $tags);
    }
    /**
     * Find a category by its id
     *
     * @param int $id
     * @return array Return a Category object
     */
    public function findById($id) {
        return Record::findByIdFrom('Category', $id);
    }

    public function findByTags($tags) {
        $categories = Record::findAllFrom('Category');
        $tagged = array();
        foreach($categories as $category) {
            $t1 = explode(',', $category->getTags());
            $t2 = explode(',', $tags);
            if (array_intersect($t1, $t2)) {
                $tagged[] = $category;
            }
        }
        return $tagged;
    }
}

?>
